/*
 * Copyright (c) 2008-2012
 * LANIT
 * All rights reserved.
 *
 * This product and related documentation are protected by copyright and
 * distributed under licenses restricting its use, copying, distribution, and
 * decompilation. No part of this product or related documentation may be
 * reproduced in any form by any means without prior written authorization of
 * LANIT and its licensors, if any.
 *
 * $Id$
 */

import java.io.*;

/**
 * todo Document type FileFactory
 */
public class FileFactory {

    private File file;
    private PrintWriter printWriter;

    public boolean isExistFile(String distFile) throws IOException {

        file = new File(distFile);
        return file.exists();
    }

    public String writeToFile(String distFile, String strWrite, boolean appendMode) throws IOException {
        file = new File(distFile);
        String description;
        String inputString;

        if (!file.exists()) {
            description = "Вы хотите создать файл с именем '" + distFile + "'? [y/n]";
            inputString = Input.fromIn(description);

            if (inputString.equals("n")) {
                Println.toOut("The end.");
                System.exit(445);
            }

            try {
                file.createNewFile();
            } catch (IOException ioe) {
                ioe.printStackTrace();
                System.exit(1);
            }
            printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, false)));
            Println.toOut(distFile + " was create.");
        } else {
            printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, appendMode)));
            Println.toOut(distFile + " is exists.");
        }

        printWriter.write(strWrite);
        printWriter.close();
        return "Done writeToFile(String distFile, String str)";
    }

    public String readFromFile(String distFile) {

        StringBuffer stringBuffer = new StringBuffer();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(distFile)));
            while (bufferedReader.ready()) {
                stringBuffer.append(bufferedReader.readLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuffer.toString();
    }
    public Reader getReader(String distFile) throws FileNotFoundException {
         return new FileReader(distFile);

     }
    public InputStream getInputStream(String distFile) throws FileNotFoundException {
        return new FileInputStream(distFile);
    }


}
